import axios from 'axios';
import { message } from 'ant-design-vue';

export const service = axios.create({
  // baseURL: './',
  timeout: 60000,
  withCredentials: true // send cookies when cross-domain requests
})
// Response interceptors
service.interceptors.request.use(
  (config) => {
    // const token = sessionStorage.getItem('token');
    // if (token) config.headers.token = token;
    config.headers.token = sessionStorage.getItem('token');
    return config;
  },
  (error) => Promise.reject(error)
);
// Response interceptors
service.interceptors.response.use(
  (response) => {
    debugger
    /* 收集待优化接口 */
    const { config } = response
    /* 对响应较慢的接口进行记录 */
    if (config.reqTime) {
      const reqDuration = new Date().getTime() - config.reqTime
      config.reqDuration = reqDuration
      /* 对响应🌍时间大于5000ms的接口进行记录 */
      if (reqDuration >= 5000) {
        console.warn(`以下接口响应缓慢，用时：${reqDuration}ms`, config.url)
      }
    }
    /* 对接口相应进行处理 */
    const res = response.data
    if (res['code'] === 401) {
      window.location.href = window.g.loginUrl + '?source=' + window.location.origin;
    } else if (res['resCode'] !== '00000') {

    }
    return res
  },
  (error) => {
    message.error('网络错误，请稍后重试，或联系管理员')
    return Promise.reject(error)
  }
)
export const BasePath = window.g.baseApiUrl;

export default service
